package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.*
import com.ruoyi.back.domain.MemberNotice
import com.ruoyi.back.enums.NoticeTypeEnum
import com.ruoyi.back.mapper.MemberNoticeMapper
import com.ruoyi.back.service.MemberNoticeService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service

@Service
class MemberNoticeServiceImpl : ServiceImpl<MemberNoticeMapper, MemberNotice>(), MemberNoticeService {

    override fun saveMemberNotice(memberNotice: MemberNotice): Data<Boolean> = run {


        memberNotice.id?.run {

            getById(this)?.run {
                if (memberNotice.noticeType != this.noticeType) {
                    remove(
                        KtQueryWrapper(MemberNotice::class.java).eq(
                            MemberNotice::noticeType,
                            memberNotice.noticeType
                        )
                    )
                }
                Ok("修改成功", updateById(memberNotice))
            }

        } ?: kotlin.run {
            remove(KtQueryWrapper(MemberNotice::class.java).eq(MemberNotice::noticeType, memberNotice.noticeType))
            Ok("新增成功", save(memberNotice))
        }
    }


    override fun memberNoticePage(): PageData<MemberNotice> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(
                Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()),
                KtQueryWrapper(MemberNotice::class.java)
                    .orderByDesc(MemberNotice::createTime)
            ).pageInfo()
        }

    override fun removeMemberNotice(ids: String): Data<Boolean> =
        Ok("批量移除成功", removeByIds(ids.split(",")))
}